//
//  SACommonProblemsVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/1/5.
//

#import "SACommonProblemsVC.h"
#import "SASystemFaqTypeListAPI.h"
#import "SACommonProblemsCell.h"
#import "SACommonProblemsModel.h"
#import "SACommonProblemsDetailVC.h"

@interface SACommonProblemsVC ()<UITextFieldDelegate,HTTPAPIDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SASystemFaqTypeListAPI *systemFaqTypeListAPI;
@property (nonatomic, strong) NSMutableArray<SACommonProblemsModel *> *dataMarr;

@end

@implementation SACommonProblemsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.systemFaqTypeListAPI == api) {
        self.dataMarr = [SACommonProblemsModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [self.tableView reloadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {

}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommonProblemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SACommonProblemsCell" forIndexPath:indexPath];
    cell.model = self.dataMarr[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SACommonProblemsDetailVC *pushVC = [[SACommonProblemsDetailVC alloc] initWithProblemsModel:self.dataMarr[indexPath.section]];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(1);
}

#pragma mark - textField
- (void)textFieldEditChanged:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchProduct2:) object:textField];
    [self performSelector:@selector(searchProduct2:) withObject:textField afterDelay:0.3];
}

- (void)searchProduct2:(UITextField *)textField {
    self.systemFaqTypeListAPI.keyword = textField.text;
    [self.systemFaqTypeListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"常见问题"];
    [self setBgImage:nil];
    self.view.backgroundColor = kLineColor;
    
    [self.view addSubview:self.whiteView];
    [self.view addSubview:self.searchTf];
    [self.view addSubview:self.tableView];
    
    [self.systemFaqTypeListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 60)];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(30, StatusHeight + 10, 315, 40)];
        _searchTf.placeholder = @"输入问题";
        _searchTf.backgroundColor = kLineColor;
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 40)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 12, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        _searchTf.returnKeyType = UIReturnKeyDone;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _searchTf;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(65 + StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(65 + StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kLineColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SACommonProblemsCell class] forCellReuseIdentifier:@"SACommonProblemsCell"];
    }
    return _tableView;
}

-(SASystemFaqTypeListAPI *)systemFaqTypeListAPI {
    if (!_systemFaqTypeListAPI) {
        _systemFaqTypeListAPI = [SASystemFaqTypeListAPI apiWithDelegate:self];
    }
    return _systemFaqTypeListAPI;
}

-(NSMutableArray<SACommonProblemsModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

@end
